package cn.yunyichina.scheduler.jobs.utils;

import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.iface.entity.base.Rule;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 规则查询工具类
 * Created by Administrator on 2017/8/2.
 */
public class RuleUtil {
    private static final Logger logger = LoggerFactory.getLogger(RuleUtil.class);
    public final static String DRUG_AUTO_DEAL_TYPE ="drugAutoDealType";//号源缓存过期时间

    public static String getRule(String hospitalCode,String attributeName,String defaultValue){

        String value = null;
        Map<String,String> ruleMap = new HashMap<String,String>();
        ruleMap.put("attribute_name" , attributeName);
        ruleMap.put("org_code" , hospitalCode);
        Response ruleResp = BaseService.callHumpFromInternal("yyt.base.rule.select", JSON.toJSONString(ruleMap));
        logger.info("RuleUtil yyt.base.rule.select response=" + JsonUtils.toJsonString(ruleResp));
        if("0".equals(ruleResp.getResultCode())){
            Rule rule=JsonUtils.parseObject(ruleResp.getResult(), Rule.class);
            if(rule != null && attributeName.equals(rule.getAttributeName())){
                value = rule.getAttributeProperties();
            }
        }

        if(StringUtils.isBlank(value)){
            value = defaultValue;
        }
        return value;
    }
}
